<?php
/**
 * tpshop
 * ============================================================================
 * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * Author: IT宇宙人
 * Date: 2015-09-09
 */
namespace app\common\model;
use app\common\logic\FlashSaleLogic;
use app\common\logic\GroupBuyLogic;
use think\Model;
use app\common\logic\PromGoodsLogic;
class Column extends Model {

    static private $instance = null;

    //自定义初始化
    protected static function init()
    {
        //TODO:自定义的初始化
    }

    /**
     * 获取所有类别
     * @param  array $search
     * @return array
     */
    public function getAllColumnList($search = []){

        $where = '1=1 ';
        if ( ! empty($search['status'])){
            $where .= ' and status ='.$search['status'];
        }
        if ( ! empty($search['nid'])){
            $where .= ' and id !='.$search['nid'];
        }
        if ( ! empty($search['tid'])){
            $where .= ' and id ='.$search['tid'].' or pid ='.$search['tid'].' and '.$where;
        }

        $data = [];

        $tA = M('column')
            ->field('id,name,pid,type,link,pic,status')
            ->where($where)
            ->order('id asc')
            ->select();

        $levelKey = 0;
        foreach ($tA as $k => $r){
            $r['typeName'] = C('column_list')[$r['type']];
            $r['statusName'] = C('status_name')[$r['status']];
            if($r['pid'] ==  0){
                $data[$levelKey] = $r;

                $levelKey1 = 0;
                foreach ($tA as $r1){
                    $r1['typeName'] = C('column_list')[$r1['type']];
                    $r1['statusName'] = C('status_name')[$r1['status']];
                    if($r['id'] == $r1['pid']){
                        $data[$levelKey]['child'][$levelKey1] = $r1;

                        $levelKey2 = 0;
                        foreach ($tA as $k2 => $r2){
                            $r2['typeName'] = C('column_list')[$r2['type']];
                            $r2['statusName'] = C('status_name')[$r2['status']];
                            if($r1['id'] == $r2['pid']){
                                $data[$levelKey]['child'][$levelKey1]['child'][$levelKey2] = $r2;

                                $levelKey2++;
                            }
                        }

                        $levelKey1++;
                    }
                }

                $levelKey++;
            }
        }

        return $data;
    }

    /**
     * 单例实例
     */
    public static function getInstance(){

        if(self::$instance == null){
            self::$instance = new self();
        }

        return self::$instance;
    }
}
